package com.gitee.zycra.future.job.common.param;

import com.gitee.zycra.future.job.common.annotation.EnumCheck;
import com.gitee.zycra.future.job.common.enums.JobCycleEnum;
import com.gitee.zycra.future.job.common.model.JobMetaData;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * @author zycra
 */
@Getter
@Setter
@ToString
@Accessors(chain = true)
public class UpdateMetaDataParam {

    /**
     * 主键
     */
    @NotNull(message = "主键不能为空")
    private Long id;

    /**
     * 应用名称
     */
    @NotNull(message = "应用名称不能为空")
    private String applicationName;

    /**
     * 任务类名
     */
    @NotNull(message = "任务类名不能为空")
    private String jobClassName;

    /**
     * 任务名称
     */
    @NotNull(message = "任务名称不能为空")
    private String jobName;

    /**
     * 失败重试次数
     */
    @NotNull(message = "失败重试次数不能为空")
    @Min(0)
    private Integer jobRetryTimes;

    /**
     * 失败重试间隔(分钟)
     */
    @NotNull(message = "失败重试间隔不能为空")
    @Min(0)
    private Integer jobRetryInterval;

    /**
     * 执行周期
     *
     * @see com.gitee.zycra.future.job.common.enums.JobCycleEnum
     */
    @NotNull(message = "执行周期不能为空")
    @EnumCheck(JobCycleEnum.class)
    private Integer jobCycle;

    public static JobMetaData buildJobMetaData(UpdateMetaDataParam updateMetaDataParam) {
        if (updateMetaDataParam == null) {
            return null;
        }
        return new JobMetaData()
                .setId(updateMetaDataParam.getId())
                .setApplicationName(updateMetaDataParam.getApplicationName())
                .setJobClassName(updateMetaDataParam.getJobClassName())
                .setJobName(updateMetaDataParam.getJobName())
                .setJobRetryTimes(updateMetaDataParam.getJobRetryTimes())
                .setJobRetryInterval(updateMetaDataParam.getJobRetryInterval())
                .setJobCycle(updateMetaDataParam.getJobCycle());
    }
}
